package com.example.work.mapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.mapper.DivisionAssetInfoCopyMapper;
import com.example.mapper.DivisionAssetInfoMapper;
import com.example.mapper.DivisionAssetIpPortInfoMapper;
import com.example.work.check.ComparisonResult;
import com.example.work.check.UpdateRecord;
import com.example.work.entiy.DivisionAssetInfo;
import com.example.work.entiy.DivisionAssetInfoCopy;
import com.example.work.entiy.DivisionAssetIpPortInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.work.entiy.DivisionAssetInfoWithIpPorts;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;


@Service
public class AssetDatabaseService {
    
    @Autowired
    private DivisionAssetInfoMapper divisionAssetInfoMapper;

    @Autowired
    private DivisionAssetInfoCopyMapper dividerAssetInfoCopyMapper;
    
    @Autowired
    private DivisionAssetIpPortInfoMapper divisionAssetIpPortInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    public void changeAssetsToDatabase(ComparisonResult result) {

    }

    /**
     * 直接插入资产数据到数据库
     * 使用事务确保数据一致性
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertAssetsToDatabase(ComparisonResult result) {
        // 1. 准备division_asset_info表的数据

    /*    for (DivisionAssetInfoWithIpPorts asset : result.getInsertRecords()) {
            // 转换为DivisionAssetInfo对象
//            DivisionAssetInfo assetInfo = convertToDivisionAssetInfo(asset);
            DivisionAssetInfoCopy divisionAssetInfoCopy = new DivisionAssetInfoCopy();
            BeanUtils.copyProperties(asset, divisionAssetInfoCopy);
            divisionAssetInfoCopy.setInsertType("1");
            dividerAssetInfoCopyMapper.insert(divisionAssetInfoCopy);

            // 1. 准备division_asset_info表的数据
//            List<DivisionAssetIpPortInfo> ipPortInfoList = new ArrayList<>();

            // 转换为DivisionAssetIpPortInfo对象
*//*            if (asset.getAssetIp() != null && asset.getPorts() != null && !asset.getPorts().isEmpty()) {
                String[] portArray = asset.getPorts().split(",");
                for (String port : portArray) {
                    DivisionAssetIpPortInfo ipPortInfo = convertToDivisionAssetIpPortInfo(asset, port.trim());
                    ipPortInfoList.add(ipPortInfo);
                }
                divisionAssetIpPortInfoMapper.batchInsert(ipPortInfoList);
            }*//*
        }*/

        // 创建临时列表存储待插入的数据
        List<DivisionAssetInfoCopy> batchInsertList = new ArrayList<>();
        int batchSize = 200; // 每批插入的数量

        for (DivisionAssetInfoWithIpPorts asset : result.getInsertRecords()) {
            // 转换为DivisionAssetInfoCopy对象
            DivisionAssetInfoCopy divisionAssetInfoCopy = new DivisionAssetInfoCopy();
            BeanUtils.copyProperties(asset, divisionAssetInfoCopy);
            divisionAssetInfoCopy.setInsertType("1");

            // 添加到批次列表
            batchInsertList.add(divisionAssetInfoCopy);

            // 当达到批次大小时，执行批量插入
            if (batchInsertList.size() >= batchSize) {
                dividerAssetInfoCopyMapper.batchInsertDivisionAssetInfo(batchInsertList);
                batchInsertList.clear(); // 清空列表，准备下一批
            }

        }
        if (!batchInsertList.isEmpty()) {
            dividerAssetInfoCopyMapper.batchInsertDivisionAssetInfo(batchInsertList);
        }


        List<DivisionAssetInfoWithIpPorts> updateRecords = result.getUpdateRecords();
        for (DivisionAssetInfoWithIpPorts updateRecord : updateRecords) {

            DivisionAssetInfoCopy divisionAssetInfoCopy = new DivisionAssetInfoCopy();
            BeanUtils.copyProperties(updateRecord, divisionAssetInfoCopy);
            divisionAssetInfoCopy.setInsertType("2");
            dividerAssetInfoCopyMapper.insert(divisionAssetInfoCopy);


/*            DivisionAssetInfo entity = new DivisionAssetInfo();
            entity.setId(updateRecord.getId());
            entity.setFoundTime(updateRecord.getFoundTime());
            entity.setDivisionAssetId(updateRecord.getDivisionAssetId());
            divisionAssetInfoMapper.updateById(entity);


            DivisionAssetIpPortInfo divisionAssetIpPortInfo =  new DivisionAssetIpPortInfo();
            divisionAssetIpPortInfo.setDivisionAssetId(updateRecord.getDivisionAssetId());
            LambdaUpdateWrapper<DivisionAssetIpPortInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(DivisionAssetIpPortInfo::getGroupAssetId, updateRecord.getOldGroupAssetId());
            updateWrapper.eq(DivisionAssetIpPortInfo::getDivisionAssetId, updateRecord.getOldDivisionAssetId());
            divisionAssetIpPortInfoMapper.update(divisionAssetIpPortInfo, updateWrapper);*/

        }

    }
    
    /**
     * 将DivisionAssetInfoWithIpPorts转换为DivisionAssetInfo
     */
    private DivisionAssetInfo convertToDivisionAssetInfo(DivisionAssetInfoWithIpPorts asset) {
        DivisionAssetInfo assetInfo = new DivisionAssetInfo();
        // 复制所有division_asset_info表的字段
        assetInfo.setRelaId(asset.getRelaId());
        assetInfo.setGroupAssetId(asset.getGroupAssetId());
        assetInfo.setDivisionAssetId(asset.getDivisionAssetId());
        assetInfo.setIspCode(asset.getIspCode());
        assetInfo.setOrgCode(asset.getOrgCode());
        assetInfo.setIsAccess(asset.getIsAccess());
        assetInfo.setNetPosition(asset.getNetPosition());
        assetInfo.setAssetName(asset.getAssetName());
        assetInfo.setAssetType(asset.getAssetType());
        assetInfo.setReportAssetType(asset.getReportAssetType());
        assetInfo.setStatus(asset.getStatus());
        assetInfo.setLocation(asset.getLocation());
        assetInfo.setAssetTag(asset.getAssetTag());
        assetInfo.setAssetModel(asset.getAssetModel());
        assetInfo.setAssetVersion(asset.getAssetVersion());
        assetInfo.setManufacturer(asset.getManufacturer());
        assetInfo.setBrandName(asset.getBrandName());
        assetInfo.setBrandRegion(asset.getBrandRegion());
        assetInfo.setCpuManufacturer(asset.getCpuManufacturer());
        assetInfo.setCpuModel(asset.getCpuModel());
        assetInfo.setMemoryManufacturer(asset.getMemoryManufacturer());
        assetInfo.setMemoryModel(asset.getMemoryModel());
        assetInfo.setChipManufacturer(asset.getChipManufacturer());
        assetInfo.setChipModel(asset.getChipModel());
        assetInfo.setOpenSource(asset.getOpenSource());
        assetInfo.setOpenSourceModel(asset.getOpenSourceModel());
        assetInfo.setOpenSourceVerison(asset.getOpenSourceVerison());
        assetInfo.setSystemName(asset.getSystemName());
        assetInfo.setObejectName(asset.getObejectName());
        assetInfo.setObejectLevel(asset.getObejectLevel());
        assetInfo.setNetworkUnitType(asset.getNetworkUnitType());
        assetInfo.setFoundType(asset.getFoundType());
        assetInfo.setFoundTime(asset.getFoundTime());
        assetInfo.setVirtualTag(asset.getVirtualTag());
        assetInfo.setSourcepoolName(asset.getSourcepoolName());
        assetInfo.setCreateTime(asset.getCreateTime());
        assetInfo.setUpdateTime(asset.getUpdateTime());
        assetInfo.setImportant(asset.getImportant());
        assetInfo.setNetworkKeypoint(asset.getNetworkKeypoint());
        assetInfo.setSubdivisionType(asset.getSubdivisionType());
        assetInfo.setManageMode(asset.getManageMode());
        assetInfo.setUsage(asset.getUsage());
        assetInfo.setManagerName(asset.getManagerName());
        assetInfo.setManagerMobile(asset.getManagerMobile());
        return assetInfo;
    }
    
    /**
     * 将DivisionAssetInfoWithIpPorts和端口转换为DivisionAssetIpPortInfo
     */
    private DivisionAssetIpPortInfo convertToDivisionAssetIpPortInfo(DivisionAssetInfoWithIpPorts asset, String port) {
        DivisionAssetIpPortInfo ipPortInfo = new DivisionAssetIpPortInfo();
        ipPortInfo.setGroupAssetId(asset.getGroupAssetId());
        ipPortInfo.setDivisionAssetId(asset.getDivisionAssetId());
        ipPortInfo.setAssetIp(asset.getAssetIp());
        ipPortInfo.setPort(Integer.parseInt(port)); // 注意：端口是整数
        ipPortInfo.setIpType(asset.getIpType());
        ipPortInfo.setIsAccess(asset.getIsAccess());
        ipPortInfo.setManageTag(asset.getManageTag());
        ipPortInfo.setNetPosition(asset.getNetPosition());
        return ipPortInfo;
    }
}